package common

import (
	"fmt"

	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
	dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
	"github.com/alibabacloud-go/tea/tea"
	"github.com/spf13/viper"
)

/**
 * 使用AK&SK初始化账号Client
 * @param accessKeyId
 * @param accessKeySecret
 * @return Client
 * @throws Exception
 */
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *dysmsapi20170525.Client, _err error) {
	fmt.Println("劲辣了")
	config := &openapi.Config{
		// 您的AccessKey ID
		AccessKeyId: accessKeyId,
		// 您的AccessKey Secret
		AccessKeySecret: accessKeySecret,
	}
	// 访问的域名
	config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
	_result, _err = dysmsapi20170525.NewClient(config)
	fmt.Println(_result)
	return _result, _err
}

func SendSms(phone string, code string) (_err error) {
	client, _err := CreateClient(tea.String(viper.GetString("sms.aliyunAk")), tea.String(viper.GetString("sms.aliyunAs")))
	if _err != nil {
		return _err
	}
	codes := "{'code':" + code + "}"
	sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
		TemplateParam: tea.String(codes),
		PhoneNumbers:  tea.String(phone),
		SignName:      tea.String(viper.GetString("sms.signName")),
		TemplateCode:  tea.String(viper.GetString("sms.loginCode")),
	}
	fmt.Println(sendSmsRequest)
	// 复制代码运行请自行打印 API 的返回值
	_, _err = client.SendSms(sendSmsRequest)
	if _err != nil {
		fmt.Print(_err)
		return _err
	}
	fmt.Print(_err)
	return _err
}
